﻿using System;
using System.Collections.Generic;
using System.Text;

namespace T.FAS.Runtime.Metadata.Dao
{
    public static class StringExtension
    {
        /// <summary>
        /// Reports the one-based index of the first occurrence of the specified Unicode
        /// </summary>
        /// <param name="str"></param>
        /// <param name="value"></param>
        /// <param name="occurTime"></param>
        /// <returns></returns>
        public static int GetSepcifiedCharIndex(this string str, char value, int occurTime = 1)
        {
            if (string.IsNullOrEmpty(str) || occurTime <= 0) return -1;
            else
            {
                int tmpIndex = 0;
                for (int i = 0; i < occurTime; i++)
                {
                    if (tmpIndex == -1 || tmpIndex >= str.Length - 1)
                        return -1;
                    tmpIndex = str.IndexOf(value, tmpIndex == 0 ? 0 : tmpIndex + 1);
                }
                return tmpIndex;
            }
        }

        /// <summary>
        /// Reports the one-based index of the first occurrence of the specified Unicode str
        /// </summary>
        /// <param name="str"></param>
        /// <param name="value"></param>
        /// <param name="occurIndex"></param>
        /// <returns></returns>
        public static string GetStrFromSpecifierCharOccurIndex(this string str, char value, int occurIndex = 1, int occurTimes = 0)
        {
            int startIndex = GetSepcifiedCharIndex(str, value, occurIndex);
            int endIndex = -1;
            if (occurTimes > 0)
            {
                endIndex = GetSepcifiedCharIndex(str, value, occurIndex + occurTimes);
            }
            if (startIndex >= 0)
            {
                if (endIndex > startIndex)
                    return str.Substring(startIndex + 1, endIndex - startIndex - 1);
                return str.Substring(startIndex + 1);
            }
            else
            {
                return default;
            }
        }

        // 检测字符串的字符集
        public static Encoding GetEncoding(this string str)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(str);

            if (bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
            {
                return Encoding.UTF8;
            }
            Encoding encoding;
            string decoded;

            try
            {
                encoding = Encoding.GetEncoding("gb2312");
                decoded = encoding.GetString(bytes);
                return encoding;
            }
            catch
            {
            }

            try
            {
                encoding = Encoding.GetEncoding("iso-8859-1");
                decoded = encoding.GetString(bytes);
                return encoding;
            }
            catch (Exception)
            {
            }

            return Encoding.Default;
        }

        /// <summary>
        /// 强制转换为UTF8字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ConvertToUTF8(this string input)
        {
            if (string.IsNullOrEmpty(input)) return input;
            try
            {
                Encoding encoding = Encoding.Default;
                byte[] bytes = encoding.GetBytes(input);

                // Check for UTF-8 BOM
                if (bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
                {
                    encoding = Encoding.UTF8;
                }
                else
                {
                    // Detect encoding from byte order mark (BOM)
                    if (bytes.Length >= 2)
                    {
                        if (bytes[0] == 0xFE && bytes[1] == 0xFF)
                        {
                            encoding = Encoding.BigEndianUnicode;
                        }
                        else if (bytes[0] == 0xFF && bytes[1] == 0xFE)
                        {
                            encoding = Encoding.Unicode;
                        }
                    }

                    // Fallback to detecting encoding by checking for invalid characters
                    if (encoding == Encoding.Default)
                    {
                        string decoded = encoding.GetString(bytes);
                        if (decoded.Contains("\uFFFD"))
                        {
                            encoding = Encoding.UTF8;
                        }
                    }
                }

                if (encoding.EncodingName == Encoding.UTF8.EncodingName)
                {
                    return input;
                }
                // Convert to UTF-8
                byte[] utf8Bytes = Encoding.Convert(encoding, Encoding.UTF8, bytes);
                return Encoding.UTF8.GetString(utf8Bytes);
            }
            catch
            {
                return input;
            }
        }
    }
}
